package cn.pku.eecs.ldn.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class ToastUtil {
	private final static String TAG = "ToastUtil";
	private final static boolean DEBUG = true;

	private static List<Toast> toasts = new ArrayList<Toast>(0);

	public static void showToastLong(Context context, int resId) {
		cancelPreviousToasts();
		Toast toast = Toast.makeText(context, resId, Toast.LENGTH_LONG);
		toast.show();
		toasts.add(toast);
	}

	public static void showToastShort(Context context, int resId) {
		cancelPreviousToasts();
		Toast toast = Toast.makeText(context, resId, Toast.LENGTH_SHORT);
		toast.show();
		toasts.add(toast);
	}

	public static void showToastShort(Context context, CharSequence text) {
		cancelPreviousToasts();
		Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
		toast.show();
		toasts.add(toast);
	}

	private static void cancelPreviousToasts() {
		if (toasts == null) {
			if (DEBUG)
				Log.d(TAG, "cancelPreviousToasts(): previous toasts is null");
			return;
		}
		for (Toast toast : toasts) {
			toast.cancel();
		}
		toasts.clear();
	}
}
